package rwt.server.handlers;

import rwt.server.RailServer;
import rwt.server.RailroadMessages.LocoSpdMsg;
import rwt.server.WebMessages.PutTrainInfo;
import rwt.server.core.*;
import rwt.server.railroad.Train;

public class LocoSpdHandler extends MessageHandler<LocoSpdMsg>
{
	@Override
	public void handle(LocoSpdMsg message, IConnection source) throws Exception
	{
		Train train = RailServer.getTrainManager().getTrainByPhysicalSlot(message.slot);
		if (train == null) {
			return;
		}
		
		// For non-emergency throttles, this information will arrive in PutTrainInformation messages,
		// so ignore this lower-level messages.
		if (!train.isEmergency()) {
			return;
		}
		
		// Create the web version of this message, and send to web client
		PutTrainInfo webMsg = new PutTrainInfo();
		webMsg.trainId = train.getTrainId();
		webMsg.speed = message.speed;
		webMsg.direction = train.getDirection();
		webMsg.light = train.getLight();
		webMsg.bell = train.getBell();
		webMsg.horn = train.getHorn();
		webMsg.mute = train.getMute();
		
		RailServer.getClientManager().sendToAll(webMsg);
	}
}
